/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import de.teamskill.pux.engine.ConfigurationPort;
import de.teamskill.pux.engine.InputPort;

/**
 * TextOverlayNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class TextOverlayNode extends AbstractImageFilter {

	/**
	 * IN_TEXT
	 */
	public static final String IN_TEXT = "TEXT";

	/**
	 * DEFAULT_FONT
	 */
	public static final Font DEFAULT_FONT = new Font("Sans", Font.PLAIN, 10);

	/**
	 * CONFIG_FONT
	 */
	public static final String CONFIG_FONT = "font";

	/**
	 * CONFIG_FONTCOLOR
	 */
	public static final String CONFIG_FONTCOLOR = "fontColor";

	/**
	 * DEFAULT_FONTCOLOR
	 */
	public static final Color DEFAULT_FONTCOLOR = new Color(255, 40, 40);

	/**
	 * CONFIG_XPOS
	 */
	public static final String CONFIG_XPOS = "xPos";

	/**
	 * DEFAULT_XPOS
	 */
	public static final Integer DEFAULT_XPOS = new Integer(5);

	/**
	 * DEFAULT_YPOS
	 */
	public static final Integer DEFAULT_YPOS = new Integer(15);

	/**
	 * CONFIG_YPOS
	 */
	public static final String CONFIG_YPOS = "yPos";

	/**
	 * text
	 */
	private InputPort text;

	/**
	 * font
	 */
	private ConfigurationPort font;

	/**
	 * fontColor
	 */
	private ConfigurationPort fontColor;

	/**
	 * xPos
	 */
	private ConfigurationPort xPos;

	/**
	 * yPos
	 */
	private ConfigurationPort yPos;

	/**
	 * TextOverlayNode Constructor
	 * 
	 */
	public TextOverlayNode() {
		this("TEXTOVERLAY" + nr++, false);
	}

	/**
	 * TextOverlayNode Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public TextOverlayNode(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	public void compute() {
		String t = text.fetchData().toString();
		BufferedImage bi = (BufferedImage) in.fetchData();
		Font f = (Font) font.fetchData();
		Color c = (Color) fontColor.fetchData();
		int x = (Integer) xPos.fetchData();
		int y = (Integer) yPos.fetchData();
		overlayText(bi.getGraphics(), t, f, c, x, y);
		out.pushData(bi);
	}

	/**
	 * overlayText
	 * 
	 * @param g
	 * @param text
	 * @param font
	 * @param color
	 * @param x
	 * @param y
	 */
	private void overlayText(Graphics g, String text, Font font, Color color, int x, int y) {
		g.setColor(color);
		g.setFont(font);
		g.drawString(text, x, y);
	}

	/**
	 * @see de.teamskill.pux.library.image.AbstractImageFilter#createPorts()
	 */
	protected void createPorts() {
		super.createPorts();
		text = addInputPort(IN_TEXT, Object.class, true);

		font = addConfigurationPort(CONFIG_FONT, Font.class, DEFAULT_FONT);
		fontColor = addConfigurationPort(CONFIG_FONTCOLOR, Color.class, DEFAULT_FONTCOLOR);
		xPos = addConfigurationPort(CONFIG_XPOS, Integer.class, DEFAULT_XPOS);
		yPos = addConfigurationPort(CONFIG_YPOS, Integer.class, DEFAULT_YPOS);
	}

	/**
	 * JavaBean-style getter-method for text
	 * 
	 * @return the text
	 */
	public InputPort getText() {
		return text;
	}

	/**
	 * /** JavaBean-style setter-method for text
	 * 
	 * @param text
	 *            the text to set
	 */
	public void setText(InputPort text) {
		this.text = text;
	}

	/**
	 * JavaBean-style getter-method for font
	 * 
	 * @return the font
	 */
	public ConfigurationPort getFont() {
		return font;
	}

	/**
	 * /** JavaBean-style setter-method for font
	 * 
	 * @param font
	 *            the font to set
	 */
	public void setFont(ConfigurationPort font) {
		this.font = font;
	}

	/**
	 * JavaBean-style getter-method for fontColor
	 * 
	 * @return the fontColor
	 */
	public ConfigurationPort getFontColor() {
		return fontColor;
	}

	/**
	 * /** JavaBean-style setter-method for fontColor
	 * 
	 * @param fontColor
	 *            the fontColor to set
	 */
	public void setFontColor(ConfigurationPort fontColor) {
		this.fontColor = fontColor;
	}

	/**
	 * JavaBean-style getter-method for xPos
	 * 
	 * @return the xPos
	 */
	public ConfigurationPort getXPos() {
		return xPos;
	}

	/**
	 * /** JavaBean-style setter-method for xPos
	 * 
	 * @param pos
	 *            the xPos to set
	 */
	public void setXPos(ConfigurationPort pos) {
		xPos = pos;
	}

	/**
	 * JavaBean-style getter-method for yPos
	 * 
	 * @return the yPos
	 */
	public ConfigurationPort getYPos() {
		return yPos;
	}

	/**
	 * /** JavaBean-style setter-method for yPos
	 * 
	 * @param pos
	 *            the yPos to set
	 */
	public void setYPos(ConfigurationPort pos) {
		yPos = pos;
	}

}
